<?php
// avoid excuted from web[http://].
if( isset($_SERVER['REQUEST_METHOD']) ) {
    echo "<html><body>Permission denied.</body></html>\n";
    exit();
}
require_once(dirname(__FILE__).'/../../../../inc/class/init.php');
WF::initCLI($argv, $argc);
//create object WFExitCode and include_file /path/to/ExitCode.php
$objExitCode = WF::getConfigFactory()->getExitCode();
$file_interface = basename(__FILE__);
/*
 * php.exe
 * upgradeWF.php $argv[0]
 * ...
 * 
 */
if($argc < 1 ){
	$objExitCode->exitCli( WFExitCode::ERROR_WRONG_PARAMETER ,$file_interface);
}else if((isset($argv[1]) && in_array ( $argv[1], array ('--help', '-help', '-h', '-?' ) ))){
	$objExitCode->exitCli( WFExitCode::SHOW_HELP ,$file_interface);
} else {
	try{
	    WF::getDataObject()->beginTransaction();
	    WF::getLoggerUpgradeWF()->info("Database Transaction Start >>> php /path/to/interface_request.php upgradeWF.php");
	    
	    // Add a hook for $argv[1]
	    // If you execute php /path/to/interface_request.php upgradeWF.php [16to20|20to20]
	    // $argv[1] = [16to20|20to20]
	    // normal case will not need to assign $argv[1] in command
	    // This is a hook for Unit Test which can manual trigger 20to20
	    $strWFFromTo = isset($argv[1]) ? $argv[1] : null;
	    WF::getWidgetFactory()->getInterface()->upgradeWF($strWFFromTo);
	    
        WF::getDataObject()->commit();
        WF::getLoggerUpgradeWF()->info("Database Transaction Commit >>> php /path/to/interface_request.php upgradeWF.php");
	    $objExitCode->exitCli(WFExitCode::SUCCESS);
	} catch (Exception $e) {
	    WF::getDataObject()->rollBack();
	    WF::getLoggerUpgradeWF()->info("Database Transaction rollBack >>> php /path/to/interface_request.php upgradeWF.php");
	    $msg = $e->getMessage();
	    WF::getLoggerUpgradeWF()->error($msg);
	    $objExitCode->exitCli(WFExitCode::ERROR_UPGRADE_WIDGET_FRAMEWORK,null,$msg);
	}
}